import Login from '@/views/Login.vue'
import NOT from '@/views/404.vue'
import store from '@/store'
import authRouter from '@/router/authRouter'
import {createRouter,createWebHashHistory,createWebHistory} from 'vue-router'

const router = createRouter({
    history: process.env.NODE_ENV === 'production' ? createWebHistory() : createWebHashHistory(),
    
    //配置路由记录
    routes: [
        { path:'/login',component:Login,name:'Login'},
        {...authRouter},
        {path:'/404',component: NOT},
        { path:'/:path(.*)',redirect:'/404'},
    ]
})

router.beforeEach((to,from,next)=>{
    // 判断用户是否登录
    // 如已登录，则添加/manage路由
    
    if(to.path.startsWith('/manage')){
        if(store.getters.isLogin){
            next();
        }else{
            router.push('/login')
        }

    }else{
        next();
    }
})

export default router